package whu.cn.whushare.util;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;

import com.androidplus.util.StringUtil;

public class ShortcutUtil {
    /**
     * @param context
     * @param name           快捷方式的名称
     * @param shortCutIntent 传递进来的Intent，如果希望能被删除，需要设置同样的Action
     * @param bitmap         快捷方式显示的图片
     * @return
     */
    public static boolean addActivityShortCut(Context context, String name, Intent shortCutIntent, Bitmap bitmap) {
        if (StringUtil.isNullOrEmpty(name) || shortCutIntent == null) {
            return false;
        }
        Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
        shortcut.putExtra("duplicate", false);

        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortCutIntent);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap);

        context.sendBroadcast(shortcut);
        return true;
    }

    public static void removeActivityShortCut(Context context, String name, Intent shortCutIntent) {
        if (StringUtil.isNullOrEmpty(name) || shortCutIntent == null) {
            return;
        }
        Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
        shortcut.putExtra("duplicate", false);

        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortCutIntent);

        context.sendBroadcast(shortcut);
    }

    /**
     * 添加快捷方式
     *
     * @param context context
     * @return if shortcut added
     */
    public static boolean addShortcut(Context context, String name, String mainActivity, int iconRes) {
        if (StringUtil.isNullOrEmpty(name) || StringUtil.isNullOrEmpty(mainActivity)) {
            return false;
        }
        String pkg = context.getPackageName();

        Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
        shortcut.putExtra("duplicate", false);
        ComponentName comp = new ComponentName(pkg, mainActivity);

        Intent shortCutIntent = new Intent(Intent.ACTION_MAIN);
        shortCutIntent.setComponent(comp);
        shortCutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        shortCutIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        shortCutIntent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortCutIntent);

        Intent.ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(context, iconRes);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);

        context.sendBroadcast(shortcut);
        return true;
    }

    /**
     * 删除当前应用的桌面快捷方式
     *
     * @param context context
     */
    public static void removeShortcut(Context context, String name, String mainActivity) {
        if (StringUtil.isNullOrEmpty(name) || StringUtil.isNullOrEmpty(mainActivity)) {
            return;
        }
        String pkg = context.getPackageName();

        Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
        shortcut.putExtra("duplicate", false);
        ComponentName comp = new ComponentName(pkg, mainActivity);

        Intent shortCutIntent = new Intent(Intent.ACTION_MAIN);
        shortCutIntent.setComponent(comp);
        shortCutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortCutIntent);

        context.sendBroadcast(shortcut);
    }

    /**
     * 判断桌面是否已添加快捷方式
     * ps: not always working on different Android system
     *
     * @param context context
     * @return if shortcut exist on screen
     */
    public static boolean hasShortcut(Context context) {
        boolean result;
        // 获取当前应用名称
        String title = null;
        try {
            final PackageManager pm = context.getPackageManager();
            title = pm.getApplicationLabel(
                    pm.getApplicationInfo(context.getPackageName(),
                            PackageManager.GET_META_DATA)).toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        result = hasShortcut(context, title);
        return result;
    }

    public static boolean hasShortcut(Context context, String key) {
        if (StringUtil.isNullOrEmpty(key))
            return false;
        boolean result = false;
        final String uriStr;
        if (android.os.Build.VERSION.SDK_INT < 8) {
            uriStr = "content://com.android.launcher.settings/favorites?notify=true";
        } else {
            uriStr = "content://com.android.launcher2.settings/favorites?notify=true";
        }
        final Uri CONTENT_URI = Uri.parse(uriStr);
        final Cursor c = context.getContentResolver().query(CONTENT_URI, null,
                "title=?", new String[]{key}, null);
        if (c != null && c.getCount() > 0) {
            c.close();
            result = true;
        }
        return result;
    }
}
